package com.xuecheng.content.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 陈竞超
 * @version 1.0
 * @qq 2250537249
 */
@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachplanDto> findTeachPlanTree(long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    /**
     * @description 报错课程计划
     * @param teachplanDto  上传的课程计划
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        //课程计划id
        Long id = teachplanDto.getId();
        //修改课程计划
        if(id!=null){
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }else{
            //取出同父同级别的课程计划数量
            int count = getTeachplanCount(teachplanDto.getCourseId(), teachplanDto.getParentid());
            Teachplan teachplanNew = new Teachplan();
            //设置排序号
            teachplanNew.setOrderby(count+1);
            BeanUtils.copyProperties(teachplanDto,teachplanNew);

            teachplanMapper.insert(teachplanNew);

        }

    }

    /**
     * @description 获取最新的排序号
     * @param courseId  课程id
     * @param parentId  父课程计划id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private int getTeachplanCount(long courseId,long parentId){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId,courseId);
        queryWrapper.eq(Teachplan::getParentid,parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }

    /**
     * @description 删除课程计划
     * @param id 课程计划id
     * @return int 最新排序号
     * @author Mr.M
     */
    @Transactional
    @Override
    public void deleteTeachplan(long id) {
        if (getTeachplanOf(id) == 0){
            teachplanMapper.deleteById(id);
            deletePlanMedia(id);
        }else {
            XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
        }
    }

    @Override
    public void deleteTeachplans(long courseId) {
        LambdaQueryWrapper<Teachplan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teachplan::getCourseId,courseId);
        teachplanMapper.delete(wrapper);
        LambdaQueryWrapper<TeachplanMedia> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(TeachplanMedia::getCourseId,courseId);
        teachplanMediaMapper.delete(wrapper1);
    }

    /**
     * @description 获取最新的排序号
     * @param id 课程id
     * @param moveType 上移或者下移
     * @return int 最新排序号
     * @author Mr.M
     */
    @Transactional
    @Override
    public void moveTeachplan(String moveType, long id) {
        LambdaQueryWrapper<Teachplan> queryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Teachplan> queryWrapper2 = new LambdaQueryWrapper<>();

        queryWrapper1.eq(Teachplan::getId,id);
        Teachplan teachplan = teachplanMapper.selectOne(queryWrapper1);

        if (moveType.equals("moveup")){
            queryWrapper2.lt(Teachplan::getOrderby,teachplan.getOrderby())
                    .eq(Teachplan::getGrade,teachplan.getGrade())
                    .eq(Teachplan::getParentid,teachplan.getParentid())
                    .eq(Teachplan::getCourseId,teachplan.getCourseId());
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper2);

            if (teachplans == null || teachplans.size() == 0){
                XueChengPlusException.cast("已经为第一条记录了，无法上移");
            }else {
                //上一行的数据
                Teachplan teachplan1 = teachplans.get(teachplans.size() - 1);
                teachplan1.setOrderby(teachplan1.getOrderby()+1);
                teachplanMapper.updateById(teachplan1);
                teachplan.setOrderby(teachplan1.getOrderby()-1);
                teachplanMapper.updateById(teachplan);
            }
        }else if (moveType.equals("movedown")){
            queryWrapper2.gt(Teachplan::getOrderby,teachplan.getOrderby())
                    .eq(Teachplan::getGrade,teachplan.getGrade())
                    .eq(Teachplan::getParentid,teachplan.getParentid())
                    .eq(Teachplan::getCourseId,teachplan.getCourseId());
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper2);

            if (teachplans == null || teachplans.size() == 0){
                XueChengPlusException.cast("已经为最后一条记录了，无法下移");
            }else {
                //上一行的数据
                Teachplan teachplan1 = teachplans.get(0);
                teachplan1.setOrderby(teachplan1.getOrderby()-1);
                teachplanMapper.updateById(teachplan1);
                teachplan.setOrderby(teachplan1.getOrderby()+1);
                teachplanMapper.updateById(teachplan);
            }
        }
    }

    @Transactional
    @Override
    public TeachplanMedia associationMedia(BindTeachplanMediaDto bindTeachplanMediaDto) {
        //教学计划id
        Long teachplanId = bindTeachplanMediaDto.getTeachplanId();
        Teachplan teachplan = teachplanMapper.selectById(teachplanId);
        if(teachplan==null){
            XueChengPlusException.cast("教学计划不存在");
        }
        Integer grade = teachplan.getGrade();
        if(grade!=2){
            XueChengPlusException.cast("只允许第二级教学计划绑定媒资文件");
        }
        //课程id
        Long courseId = teachplan.getCourseId();

        //先删除原来该教学计划绑定的媒资
        teachplanMediaMapper.delete(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getTeachplanId,teachplanId));

        //再添加教学计划与媒资的绑定关系
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setCourseId(courseId);
        teachplanMedia.setTeachplanId(teachplanId);
        teachplanMedia.setMediaFilename(bindTeachplanMediaDto.getFileName());
        teachplanMedia.setMediaId(bindTeachplanMediaDto.getMediaId());
        teachplanMedia.setCreateDate(LocalDateTime.now());
        teachplanMediaMapper.insert(teachplanMedia);
        return teachplanMedia;
    }


    /**
     * @description 获取课程计划是否有子计划
     * @param id  课程id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private int getTeachplanOf(long id){
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid,id);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }

    /**
     * @description 删除媒体表
     * @param courseId  课程id
     * @return int 最新排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private void deletePlanMedia(long courseId){
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId,courseId);
        teachplanMediaMapper.delete(queryWrapper);
    }

}

